#!/usr/bin/env python
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2008, Matt Layman"
__author__ = "Matt Layman <laymansterms.dev@gmail.com>"

import unittest
import sys
import os

sys.path += [os.path.abspath('..')]

from tests.Music_test import TestMusic

from frontend.medialibrary.music import Album, AlbumHasNoTracks
from frontend.medialibrary.music import Track

class TestAlbum(TestMusic):

    def setUp(self):
        TestMusic.setUp(self)        
        self.album = Album('album1', self.cursor)

    def tearDown(self):
        TestMusic.tearDown(self)
                
    def testAlbumConstructor(self):
        """testAlbumConstructor - Ensures that an Album object is properly
        constructed"""
        self.assertTrue(isinstance(self.album, Album))
        
    def testAlbumConstructorNot(self):
        """testAlbumConstructorNot - Ensures that an AlbumHasNoTracks exception
        is raised when the created album doesn't exist in the cache"""
        self.assertRaises(AlbumHasNoTracks, Album, 'foo', self.cursor)
        
    def testAlbumStr(self):
        """testAlbumStr - Ensures that title is returned in string
        conversion"""
        result = str(self.album)
        self.assertEqual(result, 'album1')
        
    def testAlbumGetTitle(self):
        """testAlbumGetTitle - Ensures that the album title is returned"""
        result = self.album.get_title()
        self.assertEqual(result, 'album1')
        
    def testAlbumHasAlbumArt(self):
        """testAlbumHasAlbumArt - Ensures that album art exists for the file"""
        album_art = os.path.join(self.art_path, "artist0 - album1.jpg")
        file = open(album_art, "wb")
        file.close()
        self.assertTrue(self.album.has_album_art())
        if os.path.exists(album_art):
            os.remove(album_art)
        
    def testAlumHasAlbumArtNot(self):
        """testAlbumHasAlbumArtNot - Ensures that missing album art is reported
        back"""
        otherAlbum = Album('album0', self.cursor)
        self.assertFalse(otherAlbum.has_album_art())
        
    def testAlbumGetAlbumArtUrl(self):
        """testAlbumGetAlbumArtUrl - Ensures that the path to the album's art
        is returned"""
        result = self.album.get_album_art_url();
        album_art = os.path.join(self.art_path, "artist0 - album1.jpg")
        self.assertEqual(result, album_art)
        
    def testAlbumGetAlbumArtUrlNot(self):
        #TODO: doc string for get_album_art_url mentions raising an exception.
        # I don't see it anywhere handled anywhere.
        pass
    
    def testAlbumGetTracks(self):
        """testAlbumGetTracks - Ensures that all tracks for an album are
        returned"""
        result = self.album.get_tracks()
        self.assertEqual(len(result), 4)
        for i in result:
            self.assertTrue(isinstance(i, Track))
            
    def testAlbumGetNumberOfTracks(self):
        """testAlbumGetNumberOfTracks - Ensures correct number of tracks from
        album is returned"""
        self.assertEqual(self.album.get_number_of_tracks(), 4)
        
    def testAlbumGetYear(self):
        """testAlbumGetYear - Ensures year of album is returned"""
        self.assertEqual(self.album.get_year(), 0)
        
    def testAlbumGetGenre(self):
        """testAlbumGetGenre - Ensures genre of album is returned"""
        self.assertEqual(self.album.get_genre(), 'genre0')

    def testAlbumGetArtist(self):
        """testAlbumGetArtist - Ensures artist of album is returned"""
        self.assertEqual(self.album.get_artist(), 'artist0')
        
    def testAlbumGetTotalLength(self):
        """testAlbumGetArtist - Ensures total length of album is returned"""
        self.assertEqual(self.album.get_total_length(), 8)
        
